package sebastiankerckhof.scene.transformation;

import sebastiankerckhof.math.Matrix4f;
import sebastiankerckhof.math.Vector3f;

public class Rotation implements Transformation{
		Vector3f axis;
		float angle;
		
	 public Rotation(Vector3f axis, float angle){
		 this.axis = axis;
		 this.angle = angle;
	 }
	 
	 
	 @Override
	 public Matrix4f invert(){
		 return getInvertStep(1.0f);
	 }
	 
	@Override
	public Matrix4f getInvertStep(float step) {
		return getStep(step).transpose();
	}

	@Override
  public Matrix4f getMatrix() {
	  return getMatrix(this.axis, this.angle);
  }
	
	protected Matrix4f getMatrix(Vector3f axis, float angle){
		float c = (float)Math.cos(angle);
	 	float s = (float)Math.sin(angle);
	 	Vector3f unit = axis.normalize();
   
   	float x = unit.x; float y = unit.y; float z = unit.z;
   
   	return new Matrix4f(
   			1 + (1-c)*(x*x-1),	-z*s+(1-c)*x*y,			y*s+(1-c)*x*z, 			0.0f,
   			z*s+(1-c)*x*y,			1 + (1-c)*(y*y-1),	-x*s+(1-c)*y*z,			0.0f,
   			-y*s+(1-c)*x*z,			x*s+(1-c)*y*z,			1 + (1-c)*(z*z-1),	0.0f,
   			0.0f,								0.0f, 							0.0f, 							1.0f);

	}

	@Override
  public Matrix4f getStep(float step) {
	  return getMatrix(this.axis, angle*step);
  }
	
	@Override
	public String toString(){
		return getMatrix().toString();
	}


}
